package datastructures;

import datastructures.base.Node;

public class BST {

	public Node root;
	
	public BST()
	{
		root = null;
	}
	
	public Node createNodes (int[] dataList)
	{
		for (int data : dataList)
		{
			Node newElement = new Node (data);
			
			if (root == null)
				root = newElement;
			else
				insertNode (root, newElement);
		}
		
		return root;
	}
	
	public void insertNode (Node theFlagNode, Node theNode)
	{
		// Using Recursion
//		if (theFlagNode.data > theNode.data)
//		{
//			if (theFlagNode.left == null)
//				theFlagNode.left = theNode;
//			else
//				insertNode (theFlagNode.left, theNode);
//		}
//		else if (theFlagNode.data <= theNode.data)
//		{
//			if (theFlagNode.right == null)
//				theFlagNode.right = theNode;
//			else
//				insertNode(theFlagNode.right, theNode);
//		}
		
		// Without Recursion
		Node currentNode = theFlagNode;
		while (theFlagNode != null)
		{
			currentNode = theFlagNode;
			
			if (theNode.data < theFlagNode.data)
			{
//				theFlagNode.lcount++;
				theFlagNode = theFlagNode.left;
			}
			else
				theFlagNode = theFlagNode.right;
		}
		
		// - CurrentNode will denote the node above the NULL
		if (theNode.data < currentNode.data)
			currentNode.left = theNode;
		else
			currentNode.right = theNode;
	}

}
